﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using WorkOrder.Business;

namespace WorkOrder.workBill
{
    public partial class fihishedTimelyQuery : System.Web.UI.Page
    {   private static DataTable dt;
        private TaskReport tr = new TaskReport();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlOrderType.Items.Clear();
                ddlOrderType.DataTextField = "order_type";
                ddlOrderType.DataValueField = "order_type";
                ddlOrderType.DataSource = tr.GetOrderType();
                ddlOrderType.DataBind();
                ddlOrderType.Items.Add(new ListItem("-- 请选择 --", ""));
                ddlOrderType.SelectedIndex = ddlOrderType.Items.Count - 1;

                ddlProduct.Items.Clear();
                ddlProduct.DataTextField = "product_name";
                ddlProduct.DataValueField = "product_name";
                ddlProduct.DataSource = tr.GetProduct();
                ddlProduct.DataBind();
                ddlProduct.Items.Add(new ListItem("-- 请选择 --", ""));
                ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;

                seleTown.Items.Clear();
                seleTown.DataTextField = "town_name";
                seleTown.DataValueField = "town_name";
                seleTown.DataSource = tr.GetTown();
                seleTown.DataBind();
                seleTown.Items.Add(new ListItem("-- 请选择 --", ""));
                seleTown.SelectedIndex = seleTown.Items.Count - 1;

                seleArea.Items.Clear();
                seleArea.DataTextField = "area_name";
                seleArea.DataValueField = "area_name";
                seleArea.DataSource = tr.GetArea();
                seleArea.DataBind();
                seleArea.Items.Add(new ListItem("-- 请选择 --", ""));
                seleArea.SelectedIndex = seleArea.Items.Count - 1;

                txtRow.Text = GridView1.PageSize.ToString();

                dt = null;
            }
        }

        #region 处理光标行颜色事件

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor,this.style.background='#ffe3a0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
            try
            {
                if (Convert.ToDouble(e.Row.Cells[4].Text) > 0)
                    e.Row.Cells[4].BackColor = System.Drawing.Color.Coral;

                if (e.Row.Cells[2].Text == "是")
                    e.Row.Cells[2].BackColor = System.Drawing.Color.Coral;
            }
            catch
            {
            }
        }

        #endregion

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                GridView1.PageIndex = e.NewPageIndex;
                BindGridView();
                TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
                tb.Text = (GridView1.PageIndex + 1).ToString();
            }
            catch
            {
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "go")
            {
                try
                {
                    TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
                    int num = Int32.Parse(tb.Text);
                    GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                    GridView1_PageIndexChanging(null, ea);
                }
                catch
                {
                }
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";

            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.GridView1.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }

            // 重新设定GridView排序数据列及排序方向
            GridView1.Attributes["SortExpression"] = sortExpression;
            GridView1.Attributes["SortDirection"] = sortDirection;

            BindGridView();
        }

        #region 导出到excel文件

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string tmpStr = "已完成工单超时情况查询";
            string[] title = {
                                 "工单号", "工单主题", "区域", "镇区", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间",
                                 "CRM资源确认时间"
                                 , "完成时间", "产品类型", "业务类型"
                             };
            string[] field = {
                                 "工单号", "工单主题", "区域", "镇区", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间",
                                 "CRM资源确认时间"
                                 , "完成时间", "产品类型", "业务类型"
                             };
            Export(title, field, tmpStr);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string tmpStr = "已完成工单超时情况查询";
            string[] title = {
                                 "工单号", "工单主题", "区域", "镇区", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间",
                                 "CRM资源确认时间"
                                 , "完成时间", "产品类型", "业务类型"
                             };
            string[] field = {
                                 "工单号", "工单主题", "区域", "镇区", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间",
                                 "CRM资源确认时间"
                                 , "完成时间", "产品类型", "业务类型"
                             };
            Export(title, field, tmpStr);
        }

        private void Export(string[] title, string[] field, string fileName)
        {
            if (dt != null)
            {
                ExportDsToXls(this.Page, fileName, title, field, dt);
            }
        }

        public static string ExportTable(string[] title, string[] field, DataTable table)
        {
            StringBuilder sb = new StringBuilder();

            //列名
            string tmp = "";
            for (int i = 0; i < title.Length; i++)
            {
                if (i > 0) tmp += "\t";
                tmp += title[i];
            }
            sb.AppendLine(tmp);
            tmp = null;
            title = null;

            //数据内容
            foreach (DataRow row in table.Rows)
            {
                string str = "";

                for (int j = 0; j < field.Length; j++)
                    foreach (DataColumn column in table.Columns)
                    {
                        if (column.ColumnName == field[j])
                        {
                            if (j > 0)
                                str += "\t";
                            if (DBNull.Value.Equals(row[column]))
                                str += "";
                            else if (row[column].ToString() == "False")
                                str += "否";
                            else if (row[column].ToString() == "True")
                                str += "是";
                            else str += row[column].ToString().Replace("\r", " ").Replace("\n", " ");
                        }
                    }
                sb.AppendLine(str);
                str = null;
            }
            field = null;

            return sb.ToString();
        }

        public static void ExportDsToXls(Page page, string fileName, string[] title, string[] field, DataTable table)
        {
            page.Response.Clear();
            page.Response.Buffer = true;
            page.Response.Charset = "GB2312";
            //page.Response.Charset = "UTF-8";
            page.Response.AppendHeader("Content-Disposition",
                                       "attachment;filename=" +
                                       System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) +
                                       System.DateTime.Now.ToString("_yyyyMMddHHmmss") + ".xls");
            page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //设置输出流为简体中文
            page.Response.ContentType = "application/vnd.ms-excel"; //设置输出文件类型为excel文件。 
            page.EnableViewState = false;
            page.Response.Write(ExportTable(title, field, table));
            page.Response.End();
        }

        #endregion

        protected void Button2_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        private void BindGridView()
        {
            if (Text5.Value.Trim() == "" || Text6.Value.Trim() == "")
            {    RadAjaxPanel1.ResponseScripts.Add("alert('请输入完成时间！')");return;}
            string sqlstr = "1=1";
            if (txtOrderNo.Value.Trim() != "")
                sqlstr += " and 工单号=''" + txtOrderNo.Value.Trim() + "''";
            if (txtTitle.Text.Trim() != "")
                sqlstr += " and 工单主题 like ''%" + txtTitle.Text.Trim() + "%''";
            if (ddlProduct.SelectedValue != "")
                sqlstr += " and 产品类型=''" + ddlProduct.SelectedValue + "''";
            if (ddlOrderType.SelectedValue != "")
                sqlstr += " and 业务类型=''" + ddlOrderType.SelectedValue + "''";
            if (seleTown.Value != "")
                sqlstr += " and 镇区=''" + seleTown.Value + "''";
            else if (seleArea.Value != "")
                sqlstr += " and 区域=''" + seleArea.Value + "''";
            if (Text3.Value.Trim() != "" && Text4.Value.Trim() != "")
                sqlstr += " and 派单时间 between ''" + Text3.Value.Trim() + " 0:0:0'' and ''" + Text4.Value.Trim() + " 23:59:59''";
            
            if (Radio1.Checked)
                sqlstr += " and 是否超时=''是''";
            else if (Radio2.Checked)
                sqlstr += " and 是否超时=''否''";
            if (Text1.Value.Trim() != "" && Text2.Value.Trim() != "")
                sqlstr += " and 超时时间 between ''" + Text1.Value.Trim() + " 0:0:0'' and ''" + Text2.Value.Trim() + " 23:59:59''";
            if (TextBox2.Text.Trim() != "")
            {
                try
                {
                    Convert.ToDecimal(TextBox2.Text.Trim());
                    if (CheckBox4.Checked)
                        sqlstr += CheckBox6.Checked
                                      ? " and 工单历时>=" + TextBox2.Text.Trim()
                                      : " and 工单历时>" + TextBox2.Text.Trim();
                    else if (CheckBox5.Checked)
                        sqlstr += CheckBox6.Checked
                                      ? " and 工单历时<=" + TextBox2.Text.Trim()
                                      : " and 工单历时<" + TextBox2.Text.Trim();
                    else if (CheckBox6.Checked) sqlstr += " and 工单历时=" + TextBox2.Text.Trim();
                }
                catch
                {
                    RadAjaxPanel1.ResponseScripts.Add("alert('数字输入错误！')");
                    return;
                }
            }
            if (TextBox3.Text.Trim() != "")
            {
                try
                {
                    Convert.ToDecimal(TextBox3.Text.Trim());
                    if (CheckBox7.Checked)
                        sqlstr += CheckBox9.Checked
                                      ? " and 售中在线历时>=" + TextBox3.Text.Trim()
                                      : " and 售中在线历时>" + TextBox3.Text.Trim();
                    else if (CheckBox8.Checked)
                        sqlstr += CheckBox9.Checked
                                      ? " and 售中在线历时<=" + TextBox3.Text.Trim()
                                      : " and 售中在线历时<" + TextBox3.Text.Trim();
                    else if (CheckBox9.Checked) sqlstr += " and 售中在线历时=" + TextBox3.Text.Trim();
                }
                catch
                {
                    RadAjaxPanel1.ResponseScripts.Add("alert('数字输入错误！')");
                    return;
                }
            }
           
            dt = tr.getSumList(Text5.Value.Trim(), Text6.Value.Trim(), sqlstr);
            // GridView绑定并显示数据
            GridView1.DataSource = dt;
            if (dt != null)
            {
                hidRowCount.Value = dt.Rows.Count.ToString();
                lblRowCount.Text = "共 " + dt.Rows.Count + " 条记录";
            }
            GridView1.DataBind();
        }
    }
}